package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.*;
import cn.edu.scau.dropship.service.FinanceService;
import cn.edu.scau.dropship.service.TraceService;
import cn.edu.scau.dropship.service.UserService;
import cn.edu.scau.dropship.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wallet")
public class WalletController {

    @Autowired
    private WalletService walletsService;
    @Autowired
    private UserService userService;
    @Autowired
    private FinanceService financeService;
    @Autowired
    private TraceService traceService;


    /*
     * 根据token获取用户名，根据用户名查找user
     * 根据user的部分信息填充页面，
     * 注册钱包
     * */
    @PostMapping("/register")
    @ResponseBody
    public String Rigister(HttpServletRequest request) {
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        String pw=request.getParameter("pw");

        Wallets wallets=new Wallets();
        wallets.setMoney(0.00);
        wallets.setPassword(pw);
        wallets.setUserId(uid);

        int result=walletsService.insert(wallets);
        if(result>0){
            return "true";
        }
        return "false";
    }

    /*
     * 根据uid和npw修改支付密码
     * */
    @PostMapping("/updatePw")
    @ResponseBody
    public String updatePw(HttpServletRequest request) {
        //获取uid
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        String pw=request.getParameter("pw");

        //根据用户名查找account
        Wallets wallets=walletsService.selectByUid(uid);
        wallets.setPassword(pw);

        int result=walletsService.updateByPrimaryKey(wallets);
        if(result>0){
            return "true";
        }
        return "false";
    }

    //需要事务，todo
    //品牌商，取现
    @PostMapping(value = "/cashout")
    @ResponseBody
    public String cashOut(HttpServletRequest request) {
        //uid
        Integer uid=Integer.valueOf(request.getParameter("uid"));

        //wid
        Wallets wallets=walletsService.selectByUid(uid);
        Integer wid=wallets.getId();

        //余额
        Double balance=wallets.getMoney();
        //提现金额
        Double money=Double.valueOf(request.getParameter("money"));
        //判断金额是否为负数
        if(money<0.0){
            return "请保证提现金额大于0";
        }
        //判断余额是否足够
        if(money>balance){
            return "您的余额已不足，请减少提现金额";
        }

        //输入的支付密码
        String password=request.getParameter("pw");
        //从数据库读取wid的密码
        String pw=wallets.getPassword();
        //判断支付密码是否正确
        if(!password.equals(pw)){
            return "支付密码不正确，请重新输入";
        }

        //往finance表插入
        Finance_check finance_check=new Finance_check();
        finance_check.setType("提现");
        finance_check.setMoney(money);
        //获取系统当前时间
        Date applyTime=new Date();
        finance_check.setApplyTime(applyTime);
        System.out.println(applyTime);
        finance_check.setState("待审核");

        int result = financeService.insert(finance_check);
        if(result==0){
            return "finance插入失败";
        }

        //遍历找到最大的fid，作为新增记录到的fid
        List<Finance_check> flist=financeService.getlist();
        int max=0;
        int fid=0;
        for(int i=0;i<flist.size();i++){
            if(flist.get(i).getId()>max){
                max=flist.get(i).getId();
            }
        }
        fid=max;
        System.out.println(fid);
        TraceKey traceKey=new TraceKey();
        traceKey.setFcId(fid);
        traceKey.setWalletsId(wid);
        int result2=traceService.insert(traceKey);
        if(result2==0){
            return "trace表插入失败";
        }
        return "true";
    }


    //需要事务，todo
    //借卖方，充值
    @PostMapping(value = "/cashin")
    @ResponseBody
    public String cashIn(HttpServletRequest request) {
        //uid
        Integer uid=Integer.valueOf(request.getParameter("uid"));

        //wid
        Wallets wallets=walletsService.selectByUid(uid);
        Integer wid=wallets.getId();

        //余额
        Double balance=wallets.getMoney();
        //提现金额
        Double money=Double.valueOf(request.getParameter("money"));
        //判断金额是否为负数
        if(money<0.0){
            return "请保证充值金额大于0";
        }

        //输入的支付密码
        String password=request.getParameter("pw");
        //从数据库读取wid的密码
        String pw=wallets.getPassword();
        //判断支付密码是否正确
        if(!password.equals(pw)){
            return "支付密码不正确，请重新输入";
        }

        //往finance表插入
        Finance_check finance_check=new Finance_check();
        finance_check.setType("充值");
        finance_check.setMoney(money);
        //获取系统当前时间
        Date applyTime=new Date();
        finance_check.setApplyTime(applyTime);
        System.out.println(applyTime);
        finance_check.setState("待审核");

        int result = financeService.insert(finance_check);
        if(result==0){
            return "finance插入失败";
        }

        //遍历找到最大的fid，作为新增记录到的fid
        List<Finance_check> flist=financeService.getlist();
        int max=0;
        int fid=0;
        for(int i=0;i<flist.size();i++){
            if(flist.get(i).getId()>max){
                max=flist.get(i).getId();
            }
        }
        fid=max;
        System.out.println(fid);
        TraceKey traceKey=new TraceKey();
        traceKey.setFcId(fid);
        traceKey.setWalletsId(wid);
        int result2=traceService.insert(traceKey);
        if(result2==0){
            return "trace表插入失败";
        }
        return "true";
    }


    @PostMapping(value="/selectByUname")
    @ResponseBody
    public Double selectByUname(HttpServletRequest request){
        String uname=request.getParameter("uName");
        User user=userService.selectByName(uname);
        Integer uid=user.getId();
        Wallets wallets=walletsService.selectByUid(uid);
        return wallets.getMoney();
    }


    @PostMapping(value="/selectByUid")
    @ResponseBody
    public Wallets selectByUid(HttpServletRequest request){
        //查询结果
        List<Object> list=new ArrayList<>();
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        Wallets wallets=walletsService.selectByUid(uid);
        if(wallets!=null){
            return wallets;
        }
        return null;
    }

    @PostMapping(value="/selectMyWalletByUid")
    @ResponseBody
    public String selectMyWalletByUid(HttpServletRequest request){
        //查询结果
        List<Object> list=new ArrayList<>();
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        Wallets wallets=walletsService.selectByUid(uid);
        if(wallets!=null){
            return "true";
        }
        return "false";
    }

    @PostMapping(value="/selectBalanceByUid")
    @ResponseBody
    public List<String> selectBalanceByUid(HttpServletRequest request){
        //查询结果
        List<String> list=new ArrayList<>();

        //获取账户名
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        User user=userService.selectByPrimaryKey(uid);
        list.add(user.getName());

        //获取账户余额
        Wallets wallets=walletsService.selectByUid(uid);
        String balance=String.valueOf(wallets.getMoney());
        list.add(balance);
        return list;
    }


    //根据uid查看账户记录
    @PostMapping(value="/selectListByUid")
    @ResponseBody
    public List<Finance_check> selectListByWid(HttpServletRequest request){
        //查询结果
        List<Finance_check> result=new ArrayList<>();

        //获取wid
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        Wallets wallets=walletsService.selectByUid(uid);
        Integer wid=wallets.getId();

        //根据wid获取finance_check
        List<TraceKey> tlist=traceService.selectByWid(wid);
        for(int i=0;i<tlist.size();i++){
            Integer fid=tlist.get(i).getFcId();
            Finance_check finance_check=financeService.selectByPrimaryKey(fid);
            result.add(finance_check);
        }

        return result;
    }
}
